/*
 * Copyright (c) Jan Šlahora (${USER}) - 2011.
 * Created in IntelliJ IDEA 10.5
 */

/*
 * Created by JFormDesigner on Wed Nov 30 16:04:51 CET 2011
 */

package MW;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import GUIComponents.AgregaceComponents.AgregacePanel;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
import statistics.Agregace;
import statistics.Aliterace;
import statistics.Asonance.Asonance;
import statistics.Asonance.AsonanceFixniPosun;
import statistics.Asonance.AsonanceVsechnyPosuny;
import statistics.Asonance.IAsonance;
import statistics.Pocitani;
import statistics.VelikostVyberu;
import statistics.tests.*;
import text.DejCetnosti;

/**
 * @author Karel Novak
 */
public class WindowCetnosti extends JDialog {
  JFrame okno = (JFrame) getParent();
  MW rodic = null;

  public WindowCetnosti(Frame owner) {
    super(owner);
    initComponents();
    setRodic();

    chBoxAliterace.setText(chBoxAliterace.getText() + " \"" + rodic.gettCD().getPane().getTitleAt(rodic.gettCD().getPane().getSelectedIndex()) + "\"");

    /*if (rodic.dejTexty().size() < 7) {
      chBoxAsonance.setEnabled(false);
      chBoxAsonance.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
      chBoxAsonance.setToolTipText("K provedení asonance je zapotřebí mít otevřeno alespoň sedm textů");
    }*/
    Dimension velikostP1 = panel1.getPreferredSize();
    Dimension velikostP4 = panel4.getPreferredSize();
    int wi = chBoxIgnoreSymbols.getWidth() + label3.getWidth() + spinnerAsonanceAnovaFixniPosun.getWidth() + 150;
    this.setSize((wi - 80),
        (velikostP1.height + velikostP4.height + 80));
  }


/*
  public WindowCetnosti(Dialog owner) {
    super(owner);
    initComponents();
    setRodic();

  }
*/
  private void setRodic() {
    try {
      rodic = (MW) okno;
    } catch (Exception e) {
      System.err.println("Chyba přetypování");
    }
  }

  private void button1ActionPerformed(ActionEvent e) {
    ArrayList<String> texty = rodic.dejTexty();
    String[] hlavicka = {"Znak", "Počet", "Četnost"};

    DejCetnosti.CetnostZnaku cZ = new DejCetnosti().dejCetnostZnaku(texty, false, true);

    VelikostVyberu velikostVyberu = new VelikostVyberu(cZ);
    rodic.setLabelVelVyberu(Double.toString(velikostVyberu.getN()));
    if (chBox_count_chars.isSelected()) {

      boolean ignBile, ignVelikost;
      ignBile = chBoxIgnoreSymbols.isSelected();
      ignVelikost = chBoxCaseSensitiveChars.isSelected();
      if (!ignVelikost && ignBile) {
        //..to už je spočítaný
      } else {
        cZ = new DejCetnosti().dejCetnostZnaku(texty, ignVelikost, ignBile);
      }


      rodic.addPanel("Analýza četnosti znaků", hlavicka, cZ.getHm(), true);
    }

    hlavicka[0] = "Délka slova";
    if (chBox_word_count_dle_delky.isSelected()) {

      boolean ignCisla;
      ignCisla = chBoxIgnoreNumbers_slova_dle_delky.isSelected();
      DejCetnosti.DelkaSlova dS = new DejCetnosti().dejDelkuSlova(texty, ignCisla);
      dS.run();

      rodic.addPanel("Analýza četnosti slov dle délky", hlavicka, dS.getHm());
    }

    hlavicka[0] = "Počet slov ve větě";
    if (chBox_word_count_ve_vete.isSelected()) {

      boolean ignCisla;
      ignCisla = chBoxIgnoreNumbers_slova_ve_vete.isSelected();
      DejCetnosti.SlovVeVete svv = new DejCetnosti().dejSlovVeVete(texty, ignCisla);
      svv.run();

      rodic.addPanel("Analýza četnosti vět dle počtu slov", hlavicka, svv.getHm());
    }


    hlavicka[0] = "Asonance";
    if (chBoxAsonance.isSelected()) {
      if (!chBoxAsonanceAnovaFixniPosun.isSelected() &&
          !chBoxAsonanceAnovaVsechnyPosuny.isSelected()) {
        JOptionPane.showMessageDialog(this, "Je třeba zvolit alespoň jednu možnost testování hypotézy H0.", "Chyba!", JOptionPane.ERROR_MESSAGE);
        return;
      }

      if (chBoxAsonanceAnovaFixniPosun.isSelected()) {
        try {
          int posun = new Integer(spinnerAsonanceAnovaFixniPosun.getValue().toString());

          String jmeno = new String();
          do {
            jmeno = (String)JOptionPane.showInputDialog(
                    this,
                    "Zadejte jméno výběru",
                    "Jméno výběru",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    null,
                    "");
          } while (!((jmeno != null) && (jmeno.length() > 0)));

          ((AsonanceFixniPosun) rodic.getAsonceFixniPosun()).add(new AsonanceFixniPosun(texty, posun, jmeno));


        } catch (Exception exept) {
          exept.printStackTrace();
        }

      } else if (chBoxAsonanceAnovaVsechnyPosuny.isSelected()) {
        IAsonance aVP = new AsonanceVsechnyPosuny(texty);

        String ret = "<h1>Analýza rozptylu</h1>";
        try {
          ret += "<h2>Bartlettův test</h2>";
          //..bartlet -> anova X Kruskal-Wallis
          Bartlett b = new Bartlett(aVP);
          ret += "<br>B = " + b.getB();
          double chi = new Pocitani().getChiKritickaHodnota(aVP.getaTest().getK() - 1, 0.95);
          ret += "<br>&chi;<sub>k-1</sub>(&alpha;) = &chi;<sub>" + aVP.getaTest().getN() + " -1</sub>(&alpha;) = " + chi;
          boolean HA;
          ITest test;
          if (b.getB() < chi) {
            ret += "<br><br><b>Nulová hypotéza není zamítnuta</b>";
            ret += "<h2>Anova</h2>";
            test = new Anova(aVP);

            //..test HA
            double F = (new Pocitani().getFKritickaHodnota(test.getKvel() - 1, (test.getNvel() - test.getKvel()), 0.95));
            HA = (test.getTestovaStatistika() >= F);
          } else {
            ret += "<br><br><b>Nulová hypotéza je zamítnuta</b>";
            ret += "<h2>Kruskall-Wallisův test</h2>";

            test = new KruskallWallis(aVP);
            //..test HA
            HA = (test.getTestovaStatistika() >= new Pocitani().getChiKritickaHodnota(test.getKvel() - 1, 0.95));
          }
          ret += test.toString();
          if (HA) { //..posun o sedm
            ret += "<h2>Schéffeho metoda</h2>";
            Scheffe scheffe = new Scheffe(test, aVP);
            ret += scheffe.vypis();
          }


        } catch (Exception ex) {
          ex.printStackTrace();
        }
        JEditorPane vystup = new JEditorPane();
        vystup.setContentType("text/html");
        vystup.setEditable(false);
        vystup.setText(ret);
        JScrollPane jsp = new JScrollPane(vystup);
        jsp.setName("Analýza rozptylu");
        rodic.pridejPanelTab(jsp);
      }

    }

    hlavicka[0] = "Agregace";
    if (chBoxAgregace.isSelected()) {
      Agregace a = new Agregace(texty);
      JPanel panel = new AgregacePanel(a);
      rodic.addPanel("Agregace", null, panel);
    }

    hlavicka = new String[6];
    hlavicka[0] = "Verš";
    hlavicka[1] = "Fonémy";
    hlavicka[2] = "n";
    hlavicka[3] = "k1, k2";
    hlavicka[4] = "P";
    hlavicka[5] = "KA";

    if (chBoxAliterace.isSelected()) {
      Aliterace a = new Aliterace(rodic.dejText());
      rodic.addPanel(new String("Aliterace " + rodic.gettCD().getPane().getTitleAt(rodic.gettCD().getPane().getSelectedIndex())), null, a.getTabulka());
    }

    
    this.dispose();
//    notify();
  }


  private void chBox_count_wordStateChanged(ChangeEvent e) {

    boolean b = chBox_word_count_dle_delky.isSelected();
    chBoxIgnoreNumbers_slova_dle_delky.setEnabled(b);
  }

  private void chBox_count_charsStateChanged(ChangeEvent e) {
    boolean b = chBox_count_chars.isSelected();
    chBoxCaseSensitiveChars.setEnabled(b);
    chBoxIgnoreSymbols.setEnabled(b);
  }

  private void chBox_word_count_ve_veteStateChanged(ChangeEvent e) {
    boolean b = chBox_word_count_ve_vete.isSelected();
    chBoxIgnoreNumbers_slova_ve_vete.setEnabled(b);
  }

  private void buttonStornoActionPerformed(ActionEvent e) {
    this.dispose();
  }


  private void chBoxAsonanceStateChanged(ChangeEvent e) {
    chBoxAsonanceAnovaFixniPosun.setEnabled(chBoxAsonance.isSelected());
    chBoxAsonanceAnovaVsechnyPosuny.setEnabled(chBoxAsonance.isSelected());
    label1.setEnabled(chBoxAsonance.isSelected());
    label2.setEnabled(chBoxAsonance.isSelected());
    label3.setEnabled(chBoxAsonance.isSelected());
    label4.setEnabled(chBoxAsonance.isSelected());
    spinnerAsonanceAnovaFixniPosun.setEnabled(chBoxAsonance.isSelected());

    AsonanceFixniPosun aFP = (AsonanceFixniPosun) rodic.getAsonceFixniPosun();
    if (aFP.getaTest().getMereni().size() > 0) {
      Integer posun = new Integer(aFP.getPosun());
      spinnerAsonanceAnovaFixniPosun.setValue(posun);
      spinnerAsonanceAnovaFixniPosun.setEnabled(false);
    }
  }


  private void initComponents() {
    // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    // Generated using JFormDesigner Evaluation license - Karel Novak
    ResourceBundle bundle = ResourceBundle.getBundle("cs");
    panel1 = new JPanel();
    panel2 = new JPanel();
    chBox_word_count_dle_delky = new JCheckBox();
    chBoxIgnoreNumbers_slova_dle_delky = new JCheckBox();
    chBox_word_count_ve_vete = new JCheckBox();
    chBoxIgnoreNumbers_slova_ve_vete = new JCheckBox();
    chBox_count_chars = new JCheckBox();
    chBoxCaseSensitiveChars = new JCheckBox();
    chBoxIgnoreSymbols = new JCheckBox();
    panel3 = new JPanel();
    chBoxAliterace = new JCheckBox();
    chBoxAgregace = new JCheckBox();
    chBoxAsonance = new JCheckBox();
    chBoxAsonanceAnovaVsechnyPosuny = new JCheckBox();
    label1 = new JLabel();
    label2 = new JLabel();
    chBoxAsonanceAnovaFixniPosun = new JCheckBox();
    panel5 = new JPanel();
    label3 = new JLabel();
    spinnerAsonanceAnovaFixniPosun = new JSpinner();
    label4 = new JLabel();
    panel4 = new JPanel();
    button1 = new JButton();
    buttonStorno = new JButton();

    //======== this ========
    setTitle(bundle.getString("WC.this.title"));
    setMinimumSize(new Dimension(555, 360));
    setModal(true);
    Container contentPane = getContentPane();
    contentPane.setLayout(new FormLayout(
      "default:grow",
      "top:default:grow, $lgap, 20dlu"));

    //======== panel1 ========
    {
      panel1.setBorder(new TitledBorder(bundle.getString("WC.panel1.border")));

      // JFormDesigner evaluation mark
      panel1.setBorder(new javax.swing.border.CompoundBorder(
        new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
          "JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
          javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
          java.awt.Color.red), panel1.getBorder())); panel1.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

      panel1.setLayout(new FormLayout(
        "left:default, 130dlu:grow",
        "top:default:grow"));

      //======== panel2 ========
      {
        panel2.setBorder(new TitledBorder(bundle.getString("WC.panel2.border")));
        panel2.setLayout(new FormLayout(
          "default",
          "6*(default, $lgap), default"));

        //---- chBox_word_count_dle_delky ----
        chBox_word_count_dle_delky.setText(bundle.getString("WC.chBox_word_count_dle_delky.text"));
        chBox_word_count_dle_delky.addChangeListener(new ChangeListener() {
          @Override
          public void stateChanged(ChangeEvent e) {
            chBox_count_wordStateChanged(e);
          }
        });
        panel2.add(chBox_word_count_dle_delky, CC.xy(1, 1));

        //---- chBoxIgnoreNumbers_slova_dle_delky ----
        chBoxIgnoreNumbers_slova_dle_delky.setText(bundle.getString("WC.chBoxIgnoreNumbers_slova_dle_delky.text"));
        chBoxIgnoreNumbers_slova_dle_delky.setBorder(Borders.createEmptyBorder("0dlu, 20px, 10px, 0dlu"));
        chBoxIgnoreNumbers_slova_dle_delky.setEnabled(false);
        panel2.add(chBoxIgnoreNumbers_slova_dle_delky, CC.xy(1, 3));

        //---- chBox_word_count_ve_vete ----
        chBox_word_count_ve_vete.setText(bundle.getString("WC.chBox_word_count_ve_vete.text"));
        chBox_word_count_ve_vete.addChangeListener(new ChangeListener() {
          @Override
          public void stateChanged(ChangeEvent e) {
            chBox_word_count_ve_veteStateChanged(e);
          }
        });
        panel2.add(chBox_word_count_ve_vete, CC.xy(1, 5));

        //---- chBoxIgnoreNumbers_slova_ve_vete ----
        chBoxIgnoreNumbers_slova_ve_vete.setText(bundle.getString("WC.chBoxIgnoreNumbers_slova_ve_vete.text"));
        chBoxIgnoreNumbers_slova_ve_vete.setBorder(Borders.createEmptyBorder("0dlu, 20px, 10px, 0dlu"));
        chBoxIgnoreNumbers_slova_ve_vete.setEnabled(false);
        panel2.add(chBoxIgnoreNumbers_slova_ve_vete, CC.xy(1, 7));

        //---- chBox_count_chars ----
        chBox_count_chars.setText(bundle.getString("WC.chBox_count_chars.text"));
        chBox_count_chars.addChangeListener(new ChangeListener() {
          @Override
          public void stateChanged(ChangeEvent e) {
            chBox_count_charsStateChanged(e);
          }
        });
        panel2.add(chBox_count_chars, CC.xy(1, 9));

        //---- chBoxCaseSensitiveChars ----
        chBoxCaseSensitiveChars.setText(bundle.getString("WC.chBoxCaseSensitiveChars.text"));
        chBoxCaseSensitiveChars.setBorder(Borders.createEmptyBorder("0dlu, 20px, 4px, 0dlu"));
        chBoxCaseSensitiveChars.setEnabled(false);
        panel2.add(chBoxCaseSensitiveChars, CC.xy(1, 11));

        //---- chBoxIgnoreSymbols ----
        chBoxIgnoreSymbols.setText(bundle.getString("WC.chBoxIgnoreSymbols.text"));
        chBoxIgnoreSymbols.setToolTipText(bundle.getString("WC.chBoxIgnoreSymbols.toolTipText"));
        chBoxIgnoreSymbols.setSelected(true);
        chBoxIgnoreSymbols.setBorder(Borders.createEmptyBorder("0dlu, 20px, 0dlu, 0dlu"));
        chBoxIgnoreSymbols.setEnabled(false);
        panel2.add(chBoxIgnoreSymbols, CC.xy(1, 13));
      }
      panel1.add(panel2, CC.xy(1, 1, CC.LEFT, CC.TOP));

      //======== panel3 ========
      {
        panel3.setBorder(new TitledBorder(bundle.getString("WC.panel3.border")));
        panel3.setMaximumSize(new Dimension(319, 254));
        panel3.setLayout(new FormLayout(
          "left:179dlu:grow",
          "7*(top:default, $lgap), default"));

        //---- chBoxAliterace ----
        chBoxAliterace.setText(bundle.getString("WC.chBoxAliterace.text"));
        panel3.add(chBoxAliterace, CC.xy(1, 1));

        //---- chBoxAgregace ----
        chBoxAgregace.setText(bundle.getString("WC.chBoxAgregace.text"));
        panel3.add(chBoxAgregace, CC.xy(1, 3));

        //---- chBoxAsonance ----
        chBoxAsonance.setText(bundle.getString("WC.chBoxAsonance.text"));
        chBoxAsonance.addChangeListener(new ChangeListener() {
          @Override
          public void stateChanged(ChangeEvent e) {
            chBoxAsonanceStateChanged(e);
          }
        });
        panel3.add(chBoxAsonance, CC.xy(1, 5));

        //---- chBoxAsonanceAnovaVsechnyPosuny ----
        chBoxAsonanceAnovaVsechnyPosuny.setText(bundle.getString("WC.chBoxAsonanceAnovaVsechnyPosuny.text"));
        chBoxAsonanceAnovaVsechnyPosuny.setBorder(Borders.createEmptyBorder("0dlu, 20px, 4px, 0dlu"));
        chBoxAsonanceAnovaVsechnyPosuny.setEnabled(false);
        panel3.add(chBoxAsonanceAnovaVsechnyPosuny, CC.xy(1, 7));

        //---- label1 ----
        label1.setText(bundle.getString("WC.label1.text"));
        label1.setBorder(new EmptyBorder(1, 20, 5, 0));
        panel3.add(label1, CC.xy(1, 9));

        //---- label2 ----
        label2.setText(bundle.getString("WC.label2.text"));
        label2.setBorder(new EmptyBorder(1, 38, 5, 0));
        panel3.add(label2, CC.xy(1, 11));

        //---- chBoxAsonanceAnovaFixniPosun ----
        chBoxAsonanceAnovaFixniPosun.setText(bundle.getString("WC.chBoxAsonanceAnovaFixniPosun.text"));
        chBoxAsonanceAnovaFixniPosun.setBorder(Borders.createEmptyBorder("0dlu, 20px, 4px, 0dlu"));
        chBoxAsonanceAnovaFixniPosun.setEnabled(false);
        panel3.add(chBoxAsonanceAnovaFixniPosun, CC.xy(1, 13));

        //======== panel5 ========
        {
          panel5.setLayout(new FormLayout(
            "default, $lcgap, 42dlu",
            "default:grow, $lgap, default:grow"));

          //---- label3 ----
          label3.setText(bundle.getString("WC.label3.text"));
          label3.setBorder(new EmptyBorder(1, 20, 5, 0));
          panel5.add(label3, CC.xy(1, 1));

          //---- spinnerAsonanceAnovaFixniPosun ----
          spinnerAsonanceAnovaFixniPosun.setModel(new SpinnerNumberModel(1, 1, 15, 1));
          panel5.add(spinnerAsonanceAnovaFixniPosun, CC.xy(3, 1));

          //---- label4 ----
          label4.setText(bundle.getString("WC.label4.text"));
          label4.setBorder(new EmptyBorder(1, 38, 5, 0));
          panel5.add(label4, CC.xy(1, 3));
        }
        panel3.add(panel5, CC.xy(1, 15, CC.FILL, CC.FILL));
      }
      panel1.add(panel3, CC.xy(2, 1, CC.FILL, CC.FILL));
    }
    contentPane.add(panel1, CC.xy(1, 1, CC.FILL, CC.FILL));

    //======== panel4 ========
    {
      panel4.setLayout(new FormLayout(
        "default:grow, 2*($lcgap, default)",
        "default"));

      //---- button1 ----
      button1.setText(bundle.getString("WC.button1.text"));
      button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          button1ActionPerformed(e);
        }
      });
      panel4.add(button1, CC.xy(3, 1));

      //---- buttonStorno ----
      buttonStorno.setText(bundle.getString("WC.buttonStorno.text"));
      buttonStorno.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          buttonStornoActionPerformed(e);
        }
      });
      panel4.add(buttonStorno, CC.xy(5, 1));
    }
    contentPane.add(panel4, CC.xy(1, 3, CC.FILL, CC.FILL));
    pack();
    setLocationRelativeTo(getOwner());
    // JFormDesigner - End of component initialization  //GEN-END:initComponents
  }

  // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
  // Generated using JFormDesigner Evaluation license - Karel Novak
  private JPanel panel1;
  private JPanel panel2;
  private JCheckBox chBox_word_count_dle_delky;
  private JCheckBox chBoxIgnoreNumbers_slova_dle_delky;
  private JCheckBox chBox_word_count_ve_vete;
  private JCheckBox chBoxIgnoreNumbers_slova_ve_vete;
  private JCheckBox chBox_count_chars;
  private JCheckBox chBoxCaseSensitiveChars;
  private JCheckBox chBoxIgnoreSymbols;
  private JPanel panel3;
  private JCheckBox chBoxAliterace;
  private JCheckBox chBoxAgregace;
  private JCheckBox chBoxAsonance;
  private JCheckBox chBoxAsonanceAnovaVsechnyPosuny;
  private JLabel label1;
  private JLabel label2;
  private JCheckBox chBoxAsonanceAnovaFixniPosun;
  private JPanel panel5;
  private JLabel label3;
  private JSpinner spinnerAsonanceAnovaFixniPosun;
  private JLabel label4;
  private JPanel panel4;
  private JButton button1;
  private JButton buttonStorno;
  // JFormDesigner - End of variables declaration  //GEN-END:variables
}
